<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryParmas.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="searchValueChange"
        />
        <!-- TODO: 树形组件 -->
        <el-tree
          ref="deptTree"
          node-key="id"
          :data="depts"
          :props="{ label: 'name' }"
          default-expand-all
          highlight-current
          :expand-on-click-node="false"
          @current-change="selectNode"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog = true">excel导入</el-button>

          <el-button size="mini" @click="exportExcelClick">excel导出</el-button>
        </el-row>
        <!-- TODO: 表格组件 -->
        <el-table
          :data="employeeList"
          style="width: 100%"
        >
          <el-table-column
            label="头像"
            prop="staffPhoto"
            width="90"
          >
            <template #default="{row}">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" />
              <span v-else class="username">{{ row.username.charAt(0) }} </span>
            </template>
          </el-table-column>
          <el-table-column
            label="姓名"
            prop="username"
          />
          <el-table-column
            label="手机号"
            prop="mobile"
            sortable
          />
          <el-table-column
            label="工号"
            prop="workNumber"
            sortable
          />
          <el-table-column
            label="聘用形式"
            prop="formOfEmployment"
          />
          <el-table-column
            label="部门"
            prop="departmentName"
          />
          <el-table-column
            label="入职时间"
            prop="timeOfEntry"
            sortable
          />
          <el-table-column
            label="操作"
            width="200"
          >
            <template #default="{row}">
              <el-button type="text" size="mini">查看</el-button>
              <el-button type="text" size="mini">角色</el-button>
              <el-popconfirm
                title="确定删除该员工?"
                @onConfirm="delEmployee(row)"
              >
                <el-button slot="reference" type="text" size="mini">删除</el-button>
              </el-popconfirm>
              <!-- <el-button type="text" size="mini" @click="delEmployee(row)">删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>

        <!-- TODO: 分页 -->
        <el-row type="flex" justify="end" align="middle">
          <el-pagination
            layout="total, prev, pager, next"
            :total="total"
            :page-size="queryParmas.pagesize"
            :current-page="queryParmas.page"
            @current-change="handlCurrent"
          />
        </el-row>

      </div>
    </div>
    <!-- 防止弹层 -->
    <import-excel :show-excel-dialog.sync="showExcelDialog" @getDepartmentList="getDepartmentList" />
  </div>
</template>

<script>
import { getDepartmentsApi } from '@/api/department'
import { transListToTteeData } from '@/utils'
import { deleteEmployeeApi, exportExcelApi, getEmployeeListApi } from '@/api/employee'
import FileSaver from 'file-saver'
import importExcel from './commponents/importExcel.vue'
export default {
  name: 'Employee',
  components: { importExcel },
  data() {
    return {
      // 组织架构中部门树形结构数据
      depts: [],
      // 员工列表数据的查询参数
      queryParmas: {
        page: 1,
        pagesize: 10,
        keyword: '',
        departmentId: null
      },
      // 总页数
      total: 0,
      // 部门员工列表
      employeeList: [],
      // 控制弹窗的显示与隐藏
      showExcelDialog: false
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    // 获取组织建构中部门树形结构数据
    async getDepartmentList() {
      // 调用部门列表接口方法
      // const res = await getDepartmentsApi()
      // 转树形结构
      // this.depts = transListToTteeData(res, 0)
      this.depts = transListToTteeData(await getDepartmentsApi(), 0)

      this.$nextTick(() => {
        // tree组件对象.setCurrentKey(参数的唯一标识)
        this.$refs.deptTree.setCurrentKey(this.depts[0].id)
      })
      this.queryParmas.departmentId = this.depts[0].id
      this.getEmployeeList()
    },
    // 切换选择节点是调用
    selectNode(nodeData) {
      this.queryParmas.departmentId = nodeData.id
      this.getEmployeeList()
    },
    // 获取部门的员工
    async getEmployeeList() {
      const { rows, total } = await getEmployeeListApi(this.queryParmas)
      this.employeeList = rows
      this.total = total
    },
    // 切换页面时调用
    handlCurrent(value) {
      // 更换当前用户点击的页码
      this.queryParmas.page = value
      this.getDepartmentList()
    },
    // 监听到搜索框值改变
    searchValueChange() {
      // 防抖核心代码: 利用时间差,在上一次的任务还没有开始执行是 就把他清理掉,时间间隔就会重新计算到下一周期
      clearTimeout(this.timer)
      // 提示: this..timer 仅仅是使用JS基础语法给组件实例动态设置属性
      this.timer = setTimeout(() => {
        // 页码设置为第一页
        this.queryParmas.page = 1
        // 再去查询员工
        this.getEmployeeList()
      }, 300)
    },
    // 点击Excel导出按钮
    async exportExcelClick() {
      // 调用导出员工的接口
      const res = await exportExcelApi()
      console.log(res)
      FileSaver.saveAs(res, '员工信息标.xlsx')
    },
    // 点击删除按钮
    async delEmployee(row) {
      // 调用删除接口
      await deleteEmployeeApi(row.id)
      // 提示用户删除成功
      this.$message.success('删除成功')
      // 重新加载页面时,如果当前删除的是最后一页的最后一个,需要页面减一
      if (this.employeeList.length === 1 && this.queryParmas.page > 1) {
        this.queryParmas.page--
      }
      // 重新渲染页面
      this.getDepartmentList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin: 10px;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04c9be;
      font-size: 12px;
      display: inline-block;
    }
  }
}
</style>
